package com.ls.lserp.modules.order.web;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ls.lserp.modules.sys.utils.SystemMessageUtils;
import org.apache.commons.collections.map.HashedMap;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.support.RequestContext;

import com.ls.lserp.common.config.Global;
import com.ls.lserp.common.persistence.AjaxDone;
import com.ls.lserp.common.persistence.BaseEntity;
import com.ls.lserp.common.persistence.Page;
import com.ls.lserp.common.utils.FileUtils;
import com.ls.lserp.common.utils.ResourcesUtil;
import com.ls.lserp.common.utils.StringUtils;
import com.ls.lserp.common.utils.SystemPath;
import com.ls.lserp.common.web.BaseController;
import com.ls.lserp.modules.order.entity.AuditInfo;
import com.ls.lserp.modules.order.entity.BigcargoOrder;
import com.ls.lserp.modules.order.entity.BigcargoStyle;
import com.ls.lserp.modules.order.entity.CustomizationContract;
import com.ls.lserp.modules.order.entity.DesignOption;
import com.ls.lserp.modules.order.entity.OperatingP;
import com.ls.lserp.modules.order.entity.Order;
import com.ls.lserp.modules.order.entity.Quotation;
import com.ls.lserp.modules.order.entity.SpecimenProduction;
import com.ls.lserp.modules.order.entity.StyleOperationtings;
import com.ls.lserp.modules.order.entity.UploadFileData;
import com.ls.lserp.modules.order.service.BigcargoOrderService;
import com.ls.lserp.modules.order.service.CustomizationMonitorService;
import com.ls.lserp.modules.order.service.QuotationService;
import com.ls.lserp.modules.sys.utils.UserUtils;

import freemarker.template.Configuration;
import freemarker.template.TemplateException;
import groovy.util.ResourceException;

/**
 * @author 张乃文
 * @ClassName: CustomizationMonitorController
 * @Description:
 * @date 2017年08月09日
 */

@Controller
@RequestMapping(value = "${adminPath}/customization/monitor/")
public class CustomizationMonitorController extends BaseController {

    @Autowired
    private CustomizationMonitorService cService;
    @Autowired
    Configuration freemarkerConfiguration;

  

    /**
     * 定制订单监控一览画面
     *
     * @return
     */
    @RequestMapping(value = {"monitor"})
    public String customizationMonitor() {
        return "modules/order/customizationMonitor";
    }

    /**
     * 拼接一览画面
     *
     * @param values
     * @return
     */
    @RequiresPermissions("customization:monitor:view")
    @RequestMapping(value = {"searchList"})
    @ResponseBody
    public String searchList(CustomizationContract customizationContract, HttpServletRequest request, HttpServletResponse response,
                             Model model, String[] values) throws Exception {
        Map <Object, Object> listMap = new HashMap <Object, Object> ();
        // ajax回调时使用的一览list名
        request.setAttribute ("listNames", "customizationContract");
        // 画面取得的检索项目
        String contractNO = customizationContract.getContractNO ();
        // 客户名称
        String venderName = customizationContract.getVenderName ();
        // 开始日期
        String dateStart = customizationContract.getDateStart ();
        // 截止日期
        String dateEnd = customizationContract.getDateEnd ();
        // 拼接字符串供分页使用
        String searchParam = "&contractNO=" + contractNO + "&venderName=" + venderName + "&dateStart=" + dateStart + "&dateEnd=" + dateEnd;
        //放到Attribute里，使用Global类里面的SEARCH_PAEAM作为key
        request.setAttribute (Global.SEARCH_PAEAM, searchParam);
        // 查询一览页面数据
        Page <CustomizationContract> page = cService.findCustomizationMonitorList (new Page <CustomizationContract> (request, response),
        		customizationContract);
        // 读取文件生成字符串
        String strUri = request.getContextPath () + Global.getAdminPath ();
        String strHtml = "";
        Map <String, Object> map = new HashedMap ();
        map.put ("adminUri", strUri);
        map.put ("contractList", page.getList ());
        map.put ("pageNo", page.getPageNo ());
        map.put ("pageSize", page.getPageSize ());
        map.put("springMacroRequestContext", new RequestContext (request));
        strHtml = FreeMarkerTemplateUtils
                .processTemplateIntoString (freemarkerConfiguration.getTemplate ("customizationMonitor.ftl"), map);
        listMap.put ("customizationContract", strHtml);
        listMap.put ("page", page.getHtml ());
        // ajax返回结果
        return renderString (response, listMap);
    }

    /**
     * 画面跳转用
     *
     * @param order
     * @param model
     * @return
     */

    @RequiresPermissions("customization:monitor:view")
    @RequestMapping(value = {""})
    public String list(Order order, Model model) {
        // 获取店铺信息
        List <DesignOption> designOptionList = cService.getdesignList ();
        // 获取工序信息
        List <OperatingP> OperatingPList = cService.getOperatingPList();
        // 前台select组件使用（店铺表）
        model.addAttribute ("designOptionList", designOptionList);
        
        // 前台select组件使用（工序表）
        model.addAttribute ("operatingPList", OperatingPList);
        
        model.addAttribute ("order", order);
        // 返回页面地址
        return "modules/order/customizationMonitorShow";
    }


    /**
     * @Title: 画面检索
     * @Description: 查询页面
     */

    @RequiresPermissions("customization:monitor:view")
    @RequestMapping(value = {"searchMonitorList"})
    public String searchList(Order order, HttpServletRequest request, HttpServletResponse response)
            throws ResourceException {
        Map <Object, Object> listMap = new HashMap <Object, Object> ();
        // ajax回调时使用的一览list名
        request.setAttribute (Global.LIST_NAME, "monitor");

        // 画面取得的检索项目
        //String productionNo = order.getProductionEntity().getProductionNo();
        String orderTypeCD = order.getOrderTypeCD();
        String contractNo = order.getContractNo();
        String beginDateStr = order.getBeginDateStr ();
        String orderStatusCD = order.getOrderStatusCD ();
        String orderProduceStatusCd = order.getOrderProduceStatusCd();
        String styleNo = order.getStyleNo();
        String orderNO = order.getOrderNO();
        Long orderByTerm = order.getOrderByTerm();
        Long orderByType = order.getOrderByType();
        String operating3Date = order.getOperating3Date();
        String strUri = request.getContextPath () + Global.getAdminPath ();
        // 拼接字符串供分页使用
        String searchParam = "&orderTypeCD=" + orderTypeCD +  "&contractNo=" + contractNo + "&beginDateStr=" + beginDateStr + "&orderStatusCD=" + orderStatusCD 
        		+"&orderProduceStatusCd=" + orderProduceStatusCd +  "&styleNo=" + styleNo + "&orderNO=" + orderNO + "&orderByTerm=" + orderByTerm
        		+ "&orderByType=" + orderByType + "&operating3Date=" + operating3Date;
        // 放到Attribute里，使用Global类里面的SEARCH_PAEAM作为key
        request.setAttribute (Global.SEARCH_PAEAM, searchParam);
      
        // 面料类型：面料
        order.setMaterialType (Long.parseLong (ResourcesUtil.getConfig ("material_type_material")));
        // 查询一览页面数据
        Page <Order> page = cService.findList (new Page <Order> (request, response), order);
        // 读取文件生成字符串
        String strHtml = "";
        try {
            Map <String, Object> map = new HashedMap ();
            map.put ("adminUri", strUri);
            map.put ("orders", page.getList ());
            map.put ("pageNo", page.getPageNo ());
            map.put ("pageSize", page.getPageSize ());

            strHtml = FreeMarkerTemplateUtils.processTemplateIntoString (
                    freemarkerConfiguration.getTemplate ("customizationMonitorList.ftl"), map);
        } catch (Exception e) {
        	logger.error (e.getMessage (), e);
        }

        listMap.put ("monitor", strHtml);
        listMap.put ("page", page.getHtml ());
        // ajax返回结果
        return renderString (response, listMap);
    }
    

    
}